package com.gaofei.controller;

import com.bw.activity7.activiti.pojo.ActivitiVo;
import com.bw.activity7.activiti.pojo.UserTask;
import com.bw.activity7.activiti.service.ActivitiService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class LeaveController {

    @Autowired
    ActivitiService activitiService;

    @Autowired
    TaskService taskService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    HistoryService historyService;

    //审核
    @RequestMapping("completeUserTaskById")
    public Object completeUserTaskById(@RequestBody ActivitiVo activitiVo){
//        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
//        objectObjectHashMap.put("auditFlag", activitiVo.isAuditFlag());
//        taskService.complete(activitiVo.getTaskId(), objectObjectHashMap);
        activitiService.completeUserTaskById(activitiVo);
        return "success";
    }

    @RequestMapping("queryUserTaskByUserName")
    public Object queryTask(@RequestBody ActivitiVo activitiVo){
        List<UserTask> list = new ArrayList<>();;

        List<Task> tasksList =taskService.createTaskQuery()
                .processDefinitionKey("qingjiaweb") //流程Key
                .taskAssignee(activitiVo.getUsername())//只查询该任务负责人的任务
                .list();
        for (Task task : tasksList) {
            //根据任务ID获取当前流程实例
            Map<String, Object> taskMap = new HashMap<>();
            taskMap.put("id", task.getId());
            taskMap.put("name", task.getName());
            taskMap.put("assignee", task.getAssignee());
            taskMap.put("description", task.getDescription());
            taskMap.put("processDefinitionId", task.getProcessDefinitionId());
            taskMap.put("processInstanceId", task.getProcessInstanceId());
            taskMap.put("parentTaskId", task.getParentTaskId());
            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
            UserTask userTask = new UserTask(taskMap, historicProcessInstance);
            list.add(userTask);
        }

        return list;
    }


    @RequestMapping("askForLeave")
    public String askForBiz(ActivitiVo activitiVo){
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", "zhangsan");
        //3、根据流程定义Id启动流程
        ProcessInstance processInstance =runtimeService.startProcessInstanceByKey("qingjiaweb",map);
        System.out.println("流程定义id：" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例id：" + processInstance.getId());
        System.out.println("当前活动Id：" + processInstance.getActivityId());

        return "申请请假成功!";
    }
}
